package com.abbas.smartCity.ui.parkingWhere;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;


import com.abbas.smartCity.R;
import com.abbas.smartCity.adapter.ListCommAdapter;
import com.abbas.smartCity.base.BaseActivity;
import com.abbas.smartCity.bean.BaseBean;
import com.abbas.smartCity.bean.parkingWhereBean.CarList;
import com.abbas.smartCity.retroft.ICallback;

import java.util.Map;

import retrofit2.Call;

public class AddCarActivity extends BaseActivity {

    private Button add;
    private ListView lv;
    private ListCommAdapter<CarList.RowsBean, ListCommAdapter.ParkingHistoryHolder> listAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_car;
    }

    @Override
    protected String setCustomTitle() {
        return "添加车辆";
    }

    @Override
    protected void initView() {
        add = (Button) findViewById(R.id.add);
        lv = (ListView) findViewById(R.id.lv);

    }

    @Override
    protected void initData() {
        api.getMyCar().enqueue(new ICallback<CarList>() {
            @Override
            public void onSuccess(Call<CarList> call, CarList data) {
                if (data.getRows().size() > 0) {
                    if (listAdapter == null) {
                        listAdapter = new ListCommAdapter<CarList.RowsBean, ListCommAdapter.ParkingHistoryHolder>(
                                data.getRows(),
                                ListCommAdapter.ParkingHistoryHolder.class,
                                R.layout.parking_history_item
                        ) {
                            @Override
                            protected void show(CarList.RowsBean item, View convertView, ParkingHistoryHolder viewHolder, int position, Context context) {
                                viewHolder.name.setText("所属人：" + item.getUserName());
                                viewHolder.tag1.setText("车牌号：" + item.getPlateNo());
                                viewHolder.tag2.setText("车辆类型：" + item.getType());

                                viewHolder.tag3.setVisibility(View.GONE);
                                viewHolder.money.setVisibility(View.GONE);
                            }
                        };
                        lv.setAdapter(listAdapter);
                    } else {
                        listAdapter.resetData(data.getRows());
                    }
                } else {
                    showToast("您暂时没有车辆！");
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                final LinearLayout linearLayout = new LinearLayout(context);
//                linearLayout.setOrientation(LinearLayout.VERTICAL);
//                final EditText editText = new EditText(context);
//                final LinearLayout.LayoutParams etparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//                editText.setLayoutParams(etparams);
//                linearLayout.addView(editText);
//                final Spinner spinner = new Spinner(context);
//                final LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//                spinner.setLayoutParams(spinnerParams);
                final View view = getLayoutInflater().inflate(R.layout.add_car, null, false);
                EditText et = view.findViewById(R.id.et);
                Spinner spinner = view.findViewById(R.id.spinner);

                new AlertDialog.Builder(AddCarActivity.this)
                        .setTitle("添加车辆")
                        .setView(view)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                final String plateNo = et.getText().toString();
                                final String type = spinner.getSelectedItem().toString();

                                final Map<String, Object> map = getMap();
                                map.put("plateNo",plateNo);
                                map.put("type",type);
                                api.postMyCar(createRequestBody(map)).enqueue(new ICallback<BaseBean>() {
                                    @Override
                                    public void onSuccess(Call<BaseBean> call, BaseBean data) {
                                        showToast(data.getMsg());
                                        initData();
                                    }
                                });
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .create()
                        .show();
            }
        });
    }
}